package com.lpb.spring.lpbspring.aop.pointcut;

import org.aspectj.weaver.tools.PointcutExpression;
import org.aspectj.weaver.tools.PointcutParser;
import org.aspectj.weaver.tools.ShadowMatch;

import java.lang.reflect.Method;

/**
 * @ClassName AspectJExpressionPointcut
 * @Description Aspect表达式切入点解析类
 * @date 2022/12/5 01:39
 * @Version 1.0
 * @authoe linpengbo
 */
public class AspectJExpressionPointcut implements Pointcut{

    private static PointcutParser pp = PointcutParser
            .getPointcutParserSupportingAllPrimitivesAndUsingContextClassloaderForResolution();

    //切入点表达式
    private String expression;

    //AspectJ切入点表达式 封装类
    private PointcutExpression pe;

    public  AspectJExpressionPointcut(String expression){
        this.expression=expression;
        this.pe=pp.parsePointcutExpression(expression);

    }


    @Override
    public boolean matchClass(Class<?> targetClass) throws Exception {
        return pe.couldMatchJoinPointsInType(targetClass);
    }

    @Override
    public boolean matchMethod(Class<?> targetClass, Method targetMethod) throws Exception {
        ShadowMatch sm = pe.matchesMethodExecution(targetMethod);
        return sm.alwaysMatches();
    }

    public String getExpression() {
        return expression;
    }
}
